﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace TY_Hoist.WinApp
{
    public partial class MsgTip : BaseForm
    {
        string msg = "";
        public MsgTip()
        {
            InitializeComponent();
        }
        public MsgTip(string _msg)
        {
            InitializeComponent();
            msg = _msg;
        }
        Action a1 = null;
        int maxWaitTime;
        public MsgTip(string _msg, Action _a1, int _maxWaitTime)
        {
            InitializeComponent();
            msg = _msg;
            a1 = _a1;
            maxWaitTime = _maxWaitTime;
        }
        int second = 0;
        Task t;
        private void timer1_Tick(object sender, EventArgs e)
        {
            second++;
            if (second == 1)
            {
                t = Task.Factory.StartNew(() =>
                {
                    if (a1 != null)
                    {
                        a1();
                    };
                    this.BeginInvoke(new Action(delegate
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }));

                });

            }
            if (maxWaitTime > 0)
            {
                dSkinLabel1.Text = string.Format(msg + "({0}s)", maxWaitTime - second);
                if (second >= maxWaitTime)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
        }

        private void MsgTip_Load(object sender, EventArgs e)
        {
            dSkinLabel1.Text = msg + (maxWaitTime > 0 ? string.Format("({0})s", maxWaitTime) : "");
        }

        private void MsgTip_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();
            this.Dispose();
        }
    }
}
